package Interface;

import baperjakat.ProsesAhp;
import baperjakat.Koneksi;
import dao.JabatanDao;
import dao.KriteriaDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import model.Jabatan;
import model.Kriteria;

/**
 *
 * @author Marlon
 */
public class KriteriaPanel extends javax.swing.JPanel {
    private DefaultTableModel model;
    private KriteriaDao kriteriaDao;
    private JabatanDao jabatanDao;
    private Kriteria kriteria;
    private Connection c;
    private PreparedStatement p;
    private Statement s;
    javax.swing.Timer waktu;
    
    public KriteriaPanel() throws SQLException {
        super();
        initComponents();
        
        btnTambahKriteria.setIcon(new ImageIcon((getClass().getResource("/images/Addnew toolbar.png"))));
        btnTambahKriteria.setHorizontalTextPosition(SwingConstants.CENTER);
	btnTambahKriteria.setVerticalTextPosition(SwingConstants.BOTTOM);
	btnTambahKriteria.setBounds(23, 335, 137, 55);
        
        btnHapusKriteria.setIcon(new ImageIcon((getClass().getResource("/images/Hapus.png"))));
        btnHapusKriteria.setHorizontalTextPosition(SwingConstants.CENTER);
	btnHapusKriteria.setVerticalTextPosition(SwingConstants.BOTTOM);
	btnHapusKriteria.setBounds(23, 335, 137, 55);
        
        btnHitungCR.setIcon(new ImageIcon((getClass().getResource("/images/hitung.png"))));
        btnHitungCR.setHorizontalTextPosition(SwingConstants.CENTER);
	btnHitungCR.setVerticalTextPosition(SwingConstants.BOTTOM);
	btnHitungCR.setBounds(23, 335, 137, 55);
        
        btnUbah.setIcon(new ImageIcon((getClass().getResource("/images/Ubah.png"))));
        btnUbah.setHorizontalTextPosition(SwingConstants.CENTER);
	btnUbah.setVerticalTextPosition(SwingConstants.BOTTOM);
	btnUbah.setBounds(23, 335, 137, 55);
        
        
        tampilJabatan();
        model = new DefaultTableModel();        
        model.addColumn("Kriteria");
        model.addColumn("Bobot");
        model.addColumn("Nilai");
        tabelKriteria.setModel(model);
                
        loadData();
    }
    
    private KriteriaDao getKriteriaDao() {
        if(kriteriaDao == null)
            kriteriaDao = new KriteriaDao();
        return kriteriaDao;
    }
    
    private JabatanDao getJabatanDao() {
        if(jabatanDao == null)
            jabatanDao = new JabatanDao();
        return jabatanDao;
    }

    private void tampilJabatan() throws SQLException{
        c = Koneksi.getKoneksi();
        s = c.createStatement();
        
        String sql = "select * from jabatan";
        ResultSet r = s.executeQuery(sql);
            
        cmbJabatan.addItem("= PILIH =");
        while(r.next()){
            cmbJabatan.addItem(r.getString("namajabatan"));
        }
        r.close();
        s.close();
    }
    
    private void loadData() {
        try {
            List<Kriteria> listKriteria = getKriteriaDao().getAll();
            loadData(listKriteria);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Tidak bisa mengambil data dari tabel 'Kriteria'");
            ex.printStackTrace();
        }
    }
    
    private void loadData(List<Kriteria> listKriteria) {
        model.getDataVector().removeAllElements();
        model.fireTableDataChanged();

        for(Kriteria k : listKriteria) {
            Object[] o = new Object[3];
            o[0] = k.getKriteria();
            o[1] = k.getBobot();
            o[2] = k.getNilai();
               
            model.addRow(o);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        cmbJabatan = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelKriteria = new javax.swing.JTable();
        btnHapusKriteria = new javax.swing.JButton();
        btnTambahKriteria = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtKriteria = new javax.swing.JTextField();
        txtBobot = new javax.swing.JTextField();
        txtNilai = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        btnHitungCR = new javax.swing.JButton();
        lblCr = new javax.swing.JLabel();
        btnUbah = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        setPreferredSize(new java.awt.Dimension(555, 600));

        jLabel1.setText("Pilih Jabatan :");

        cmbJabatan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbJabatanActionPerformed(evt);
            }
        });

        tabelKriteria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelKriteria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelKriteriaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelKriteria);

        btnHapusKriteria.setText("Hapus");
        btnHapusKriteria.setMaximumSize(new java.awt.Dimension(107, 23));
        btnHapusKriteria.setMinimumSize(new java.awt.Dimension(107, 23));
        btnHapusKriteria.setPreferredSize(new java.awt.Dimension(107, 23));
        btnHapusKriteria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusKriteriaActionPerformed(evt);
            }
        });

        btnTambahKriteria.setText("Tambah Kriteria");
        btnTambahKriteria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahKriteriaActionPerformed(evt);
            }
        });

        jLabel2.setText("Nama Kriteria :");

        jLabel3.setText("Bobot :");

        jLabel4.setText("Nilai :");

        txtKriteria.setPreferredSize(new java.awt.Dimension(80, 20));

        txtBobot.setPreferredSize(new java.awt.Dimension(80, 20));

        txtNilai.setPreferredSize(new java.awt.Dimension(80, 26));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText(":: Tambah Kriteria ::");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText(":: Data Kriteria Jabatan ::");

        jLabel7.setText("* Kriteria ");

        btnHitungCR.setText("Hitung CR");
        btnHitungCR.setMaximumSize(new java.awt.Dimension(107, 23));
        btnHitungCR.setMinimumSize(new java.awt.Dimension(107, 23));
        btnHitungCR.setPreferredSize(new java.awt.Dimension(107, 23));
        btnHitungCR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHitungCRActionPerformed(evt);
            }
        });

        lblCr.setText("* CR");

        btnUbah.setText("Ubah");
        btnUbah.setMaximumSize(new java.awt.Dimension(107, 23));
        btnUbah.setMinimumSize(new java.awt.Dimension(107, 23));
        btnUbah.setPreferredSize(new java.awt.Dimension(107, 23));
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCr)
                            .addComponent(jLabel7)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(cmbJabatan, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtBobot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNilai, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtKriteria, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(btnTambahKriteria)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnHapusKriteria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnUbah, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnHitungCR, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(73, 73, 73))
            .addGroup(layout.createSequentialGroup()
                .addGap(185, 185, 185)
                .addComponent(jLabel6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtBobot, txtNilai});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnHapusKriteria, btnTambahKriteria});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1))
                    .addComponent(cmbJabatan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtKriteria, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtBobot, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNilai, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCr)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnHitungCR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUbah, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTambahKriteria, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnHapusKriteria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtBobot, txtKriteria, txtNilai});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnHapusKriteria, btnHitungCR, btnTambahKriteria});

    }// </editor-fold>//GEN-END:initComponents

    private void cmbJabatanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbJabatanActionPerformed
        if(cmbJabatan.getSelectedIndex() == 0)
            return;

        jLabel7.setText("*Kriteria : "+cmbJabatan.getSelectedItem());
        String jabatan = (String)cmbJabatan.getSelectedItem();

        try {
            List<Kriteria> listKriteria = getKriteriaDao().getByJabatan(jabatan);
            loadData(listKriteria);
        } catch (SQLException ex) {
            loadData();
            JOptionPane.showMessageDialog(this, "Tidak bisa mengambil data kriteria untuk jabatan '" + jabatan + "'");
            ex.printStackTrace();
        }
    }//GEN-LAST:event_cmbJabatanActionPerformed

    private void tabelKriteriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelKriteriaMouseClicked
        int i = tabelKriteria.getSelectedRow();
        if(i == -1)
            return;
        
        kriteria = new Kriteria();
        kriteria.setKriteria((String) model.getValueAt(i, 0));
        kriteria.setBobot((String) model.getValueAt(i, 1));
        kriteria.setNilai((String) model.getValueAt(i, 2));
        
        setView(kriteria);
    }
    
    public void setView(Kriteria k){
        txtKriteria.setText(k.getKriteria());
        txtBobot.setText(k.getBobot());
        txtNilai.setText(k.getNilai());
    }//GEN-LAST:event_tabelKriteriaMouseClicked

    private void btnTambahKriteriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahKriteriaActionPerformed
        try {
            kriteria = new Kriteria();
            kriteria.setKriteria(txtKriteria.getText());
            kriteria.setBobot(txtBobot.getText());
            kriteria.setNilai(txtNilai.getText());

            String namaJabatan = cmbJabatan.getSelectedItem().toString();
            Jabatan jabatan = getJabatanDao().getByNama(namaJabatan);
            kriteria.setId(jabatan.getId());
        
            kriteriaDao.insert(kriteria);
            JOptionPane.showMessageDialog(null, "Data Kriteria Berhasil di Tambah");
            loadData();
        }catch (SQLException ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnTambahKriteriaActionPerformed

    private void btnHapusKriteriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusKriteriaActionPerformed
        if(kriteria == null)
            return;
        
        try {
            kriteriaDao.delete(kriteria);
            JOptionPane.showMessageDialog(null, "Kriteria Berhasil di Hapus");
            loadData();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }    
    }//GEN-LAST:event_btnHapusKriteriaActionPerformed

   private void HasilAhp() throws SQLException{
        System.out.println("Rasio Konsistensi seluruh Kriteria dari Jabatan : " +cmbJabatan.getSelectedItem());
        
        ProsesAhp ahp = new ProsesAhp(getKriteriaDao().getByJabatan(cmbJabatan.getSelectedItem().toString()));
        float matriksPerbandinganBerpasangan[][] = ahp.matriksPerbandinganBerpasangan();
        float matriksNilaiKriteria[][] = ahp.matriksNilaiKriteria(matriksPerbandinganBerpasangan);
        float barisPrioritas[] = ahp.barisPrioritas(matriksNilaiKriteria);
        float matriksPenjumlahanSetiapBaris[][] = ahp.matriksPenjumlahanSetiapBaris(barisPrioritas, matriksPerbandinganBerpasangan);
        float jumlahMatriksPenjumlahanSetiapBaris[] = ahp.jumlahMatriksPenjumlahanSetiapBaris(matriksPenjumlahanSetiapBaris);
        double rasioKonsistensi = ahp.rasioKonsistensi(jumlahMatriksPenjumlahanSetiapBaris, barisPrioritas);

        lblCr.setText("*Consistency Ratio : " + rasioKonsistensi);
    }
   
    private void btnHitungCRActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHitungCRActionPerformed
        try {
            HasilAhp();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Perhitungan gagal");
        }
    }//GEN-LAST:event_btnHitungCRActionPerformed

    private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
        if(kriteria == null)
            return;
        
        kriteria = new Kriteria();
        kriteria.setKriteria(txtKriteria.getText());
        kriteria.setBobot(txtBobot.getText());
        kriteria.setNilai(txtNilai.getText());
        
        try{
            kriteriaDao.update(kriteria);
            JOptionPane.showMessageDialog(null, "Berhasil di Ubah");
        }catch (SQLException ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnUbahActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHapusKriteria;
    private javax.swing.JButton btnHitungCR;
    private javax.swing.JButton btnTambahKriteria;
    private javax.swing.JButton btnUbah;
    private javax.swing.JComboBox cmbJabatan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblCr;
    private javax.swing.JTable tabelKriteria;
    private javax.swing.JTextField txtBobot;
    private javax.swing.JTextField txtKriteria;
    private javax.swing.JTextField txtNilai;
    // End of variables declaration//GEN-END:variables

}
